package com.lei.controller;

import java.util.List;

import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lei.model.ResponseEntity;
import com.lei.model.User;
import com.lei.service.UserService;


@RestController
@RequestMapping("/user")
public class UserCotroller {
	@Autowired
	private UserService userService;
	@RequestMapping(value = "/pageUser", method = RequestMethod.GET)
	public ResponseEntity<List<User>> listAllUsersPage(
			@RequestParam(name = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
		List<User> users = userService.findAllUsers();
		return new ResponseEntity<>(users);
	}
//	http://localhost:8080/mySpringMVC/user/gerUser?id=1
	@RequestMapping(value="gerUser",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<User> getUser(@RequestParam("id") long id) {
		System.out.println("Fetching User with id " + id);
		User user = userService.findById(id);
		return new ResponseEntity<>(user);
	}
	@PostMapping("/user")
	public ResponseEntity<Void> createUser(@RequestBody User user) {
		System.out.println("Creating User " + user.getName());

		if (userService.isUserExist(user)) {
			System.out.println("A User with name " + user.getName() + " already exist");
			return new ResponseEntity<Void>(ResponseEntity.StatusCode.EXISTS);
		}
		userService.saveUser(user);
		return new ResponseEntity<>();
	}
	@PutMapping
	public ResponseEntity<Void> updateUser(@RequestParam("id") long id, @RequestBody User user) {
		System.out.println("Updating User " + id);
		User currentUser = userService.findById(id);
		if (currentUser == null) {
			System.out.println("User with id " + id + " not found");
			return new ResponseEntity<>(ResponseEntity.StatusCode.NOT_FOUND);
		}
		currentUser.setName(user.getName());
		currentUser.setAge(user.getAge());
		userService.updateUser(currentUser);
		return new ResponseEntity<>();
	}
	@RequestMapping(method = RequestMethod.DELETE)
	public ResponseEntity<Void> deleteUser(@RequestParam("id") long id) {
		System.out.println("Fetching & Deleting User with id " + id);

		User user = userService.findById(id);
		if (user == null) {
			System.out.println("Unable to delete. User with id " + id + " not found");
			return new ResponseEntity<Void>(ResponseEntity.StatusCode.NOT_FOUND);
		}
		userService.deleteUserById(id);
		return new ResponseEntity<>();
	}
	
	
}
